﻿Imports Entidad
Imports Negocio
Imports Utilitario
Public Class frmLogueoInicial

    Private Sub frmLogueo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim empresa As New EmpresaNegocio
        Dim dt As DataTable = empresa.consultarEmpresas()

        cmbEmpresa.DataSource = dt
        cmbEmpresa.ValueMember = dt.Columns(0).ToString()
        cmbEmpresa.DisplayMember = dt.Columns(1).ToString()
    End Sub

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If txtUsuario.Text.Trim.Equals("") Then
            MsgBox("Ingrese Usuario")
            Exit Sub
        End If
        If txtPassword.Text.Trim.Equals("") Then
            MsgBox("Ingrese Password")
            Exit Sub
        End If
        Dim empresa As New EmpresaNegocio
        Variables.Empresa = empresa.consultarEmpresasxCodigo(cmbEmpresa.SelectedValue)
        Variables.sucursal = cmbSucursal.SelectedValue
        If IsNothing(Variables.Empresa) Then
            MsgBox("Seleccione una empresa")
            Exit Sub
        End If

        Dim login As New Utiles
        If login.autenticarUsuarioLDAP("LDAP://", "GLD", txtUsuario.Text.Trim, txtPassword.Text.Trim) Then
            Dim usuNegocio As New UsuarioNegocio
            'Variables.Usuario = usuNegocio.consultarUsuario(Variables.Empresa.gCIA_Codigo, txtUsuario.Text)
            If IsNothing(Variables.Usuario) Then
                MsgBox("Usuario NO esta registrado")
                Exit Sub
            Else
                Dim frm As New frmPrincipal
                frm.Show()
                Me.Close()
                Me.Dispose()
            End If
        Else
            MsgBox("Usuario no se encuentra registrado en la red de Gulda")
        End If
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

    Private Sub cmbEmpresa_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbEmpresa.SelectedIndexChanged
        Dim sucursal As New SucursalNegocio
        Dim dt As DataTable = sucursal.listarSucursales(cmbEmpresa.SelectedValue.ToString).Tables(0)

        cmbSucursal.DataSource = dt
        cmbSucursal.ValueMember = dt.Columns(1).ToString()
        cmbSucursal.DisplayMember = dt.Columns(2).ToString()
    End Sub
End Class
